﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using System.Xml;
using System.IO;

public partial class controls_admin_MenuFooterAction : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            readData();
            if (Request["m_id"] != null)
                loadData(Convert.ToInt32(Request["m_id"]));
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string title = txtTitle.Text.Trim();
            string linkto = txtLinkTo.Text.Trim();
            DataSet objDS = new DataSet("MenuFooters");
            if (Session["objDS"] != null)
                objDS = (DataSet)Session["objDS"];
            else
                Response.Redirect("Default.aspx?module=MenuFooter");
            DataTable objDT = objDS.Tables["MenuFooter"];
            string fname = Server.MapPath("../upload/xml/MenuFooter.xml");
            DataRow drow;
            if (objDT != null && objDT.Rows.Count > 0 && Request["m_id"] != null)
            {
                int no = Convert.ToInt32(Request["m_id"]);
                drow = objDT.Rows[no - 1];
                drow["Title"] = Server.HtmlEncode(title);
                drow["LinkTo"] = Server.HtmlEncode(linkto);
            }
            else
            {
                drow = objDT.NewRow();
                drow["Title"] = Server.HtmlEncode(title);
                drow["LinkTo"] = Server.HtmlEncode(linkto);
                objDT.Rows.Add(drow);
            }
            objDT.WriteXml(fname);
            Session.Remove("objDS");
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công.");
        }
        catch (Exception)
        {
            throw;
        }
        //lbInfo.Text = "Cập nhật thành công!";
    }
    private void readData()
    {
        string fname = Server.MapPath("../upload/xml/MenuFooter.xml");
        DataSet objDS = new DataSet("MenuFooters");
        DataTable objDT = new DataTable("MenuFooter");
        if (!File.Exists(fname))
        {
            objDT.Columns.Add("Title");
            objDT.Columns.Add("LinkTo");
            objDS.Tables.Add(objDT);
        }
        else
            objDS.ReadXml(Server.MapPath("../upload/xml/MenuFooter.xml"));
        Session.Add("objDS", objDS);
    }
    private void loadData(int no)
    {
        try
        {
            DataSet objDS = (DataSet)Session["objDS"];
            DataTable mfDT = objDS.Tables["MenuFooter"];
            if (mfDT.Rows.Count >= no)
            {
                txtTitle.Text = Server.HtmlDecode(mfDT.Rows[no - 1]["Title"].ToString());
                txtLinkTo.Text = Server.HtmlDecode(mfDT.Rows[no - 1]["LinkTo"].ToString());
            }
            else
                divMessage.InnerHtml = O2S_Message.Warning("Thông Báo", "Bản ghi này không tồn tại hoặc đã bị xóa !");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
    }
}
